package com.wenlin.myproduction.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.Group;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.wenlin.myproduction.R;
import com.wenlin.myproduction.base.BaseSwipeActivity;
import com.wenlin.myproduction.bean.NumberBean;
import com.wenlin.myproduction.binder.NumberResultBinder;
import com.wenlin.myproduction.binder.NumberSelectBinder;
import com.wenlin.myproduction.binder.NumberShowBinder;
import com.wenlin.myproduction.util.CommonCallback;
import com.wenlin.myproduction.util.MyGridLayoutManager;
import com.wenlin.myproduction.util.MyLinearLayoutManager;
import com.wenlin.myproduction.util.SortUtil;
import com.wenlin.myproduction.util.ToastUtils;
import com.wenlin.myproduction.util.UI;
import com.wenlin.myproduction.util.ValCommon;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.OnClick;
import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

public class GuessNumberMagicActivity extends BaseSwipeActivity {
    @BindView(R.id.g)
    Group group;
    @BindView(R.id.rv_1)
    RecyclerView rv1;
    @BindView(R.id.rv_2)
    RecyclerView rv2;
    @BindView(R.id.rv_3)
    RecyclerView rv3;
    @BindView(R.id.rv_4)
    RecyclerView rv4;
    @BindView(R.id.tv_result)
    TextView tvResult;

    private Items items1, items2, items3, items4;
    private MultiTypeAdapter adapter1, adapter2, adapter3, adapter4;

    private List<NumberBean> list;
    private Random random;
    private ItemTouchHelper itemTouchHelper1, itemTouchHelper2;

    public static void start(Context context) {
        Intent intent = new Intent(context, GuessNumberMagicActivity.class);
        context.startActivity(intent);
    }

    @Override
    public int getContentView() {
        return R.layout.activity_guess_number_magic;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(ValCommon.GUESS_NUMBER_MAGIC);
        //选出五个数
        items1 = new Items();
        adapter1 = new MultiTypeAdapter(items1);
        adapter1.register(NumberBean.class, new NumberSelectBinder(bean -> {
            if (items1 == null || items1.size() == 0) return;
            //改变选中
            for (int i = 0; i < items1.size(); i++) {
                Object o = items1.get(i);
                if (o instanceof NumberBean) {
                    NumberBean nb = (NumberBean) o;
                    if (nb.getId() == bean.getId()) {
                        nb.setSelected(!nb.isSelected());
                        adapter1.notifyDataSetChanged();
                        break;
                    }
                }
            }
            //检查现在选中几个数字
            if (list == null) list = new ArrayList<>();
            else list.clear();
            for (Object o :
                    items1) {
                if (o instanceof NumberBean) {
                    NumberBean nb = (NumberBean) o;
                    if (nb.isSelected()) list.add(nb);
                }
            }
            if (list.size() > 5) {
                ToastUtils.getInstance().showInfo(getResources().getString(R.string.choose_5_number_please));
                return;
            }
            if (list.size() == 5) {
                if (items2 != null) {
                    items2.clear();
                    adapter2.notifyDataSetChanged();
                }
                items2.addAll(list);
                adapter2.notifyDataSetChanged();
                if (items3 != null) {
                    items3.clear();
                    adapter3.notifyDataSetChanged();
                }
                items3.addAll(list);
                adapter3.notifyDataSetChanged();
                getResult();
                return;
            }
            if (items2 != null && items2.size() > 0) {
                items2.clear();
                adapter2.notifyDataSetChanged();
            }
            if (items3 != null && items3.size() > 0) {
                items3.clear();
                adapter3.notifyDataSetChanged();
            }
        }));
        rv1.setLayoutManager(new MyLinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        rv1.setHasFixedSize(true);
        rv1.setAdapter(adapter1);
        //第一个五位数
        items2 = new Items();
        adapter2 = new MultiTypeAdapter(items2);
        adapter2.register(NumberBean.class, new NumberShowBinder(vh -> itemTouchHelper1.startDrag(vh)));
        rv2.setLayoutManager(new MyGridLayoutManager(this, 5));
        rv2.setHasFixedSize(true);
        rv2.setAdapter(adapter2);
        itemTouchHelper1 = SortUtil.setSort(rv2, items2, adapter2, new CommonCallback() {
            @Override
            public void callback(Object bean) {
                getResult();
            }
        });
        //第二个五位数
        items3 = new Items();
        adapter3 = new MultiTypeAdapter(items3);
        adapter3.register(NumberBean.class, new NumberShowBinder(vh -> itemTouchHelper2.startDrag(vh)));
        rv3.setLayoutManager(new MyGridLayoutManager(this, 5));
        rv3.setHasFixedSize(true);
        rv3.setAdapter(adapter3);
        itemTouchHelper2 = SortUtil.setSort(rv3, items3, adapter3, new CommonCallback() {
            @Override
            public void callback(Object bean) {
                getResult();
            }
        });
        //差
        items4 = new Items();
        adapter4 = new MultiTypeAdapter(items4);
        adapter4.register(NumberBean.class, new NumberResultBinder(bean -> {
            if (items4 == null || items4.size() < 1) return;//差只有1个数字，必定是9，不考虑
            if (bean.getNumber() == 9) return;//不能选中9
            for (int i = 0; i < items4.size(); i++) {
                Object o = items4.get(i);
                if (o instanceof NumberBean) {
                    NumberBean nb = (NumberBean) o;
                    if (nb.getId() == bean.getId()) {
                        nb.setNumber(-1);
                        adapter4.notifyDataSetChanged();
                        UI.setGone(group);
                        break;
                    }
                }
            }
            //显示最终结果
            StringBuilder sb = new StringBuilder();
            for (Object o :
                    items4) {
                if (o instanceof NumberBean) {
                    NumberBean nb = (NumberBean) o;
                    if (nb.getNumber() != -1) {
                        sb.append(nb.getNumber());
                    }
                }
            }
            UI.setText(tvResult, sb.toString());
        }));
        rv4.setLayoutManager(new MyLinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        rv4.setHasFixedSize(true);
        rv4.setAdapter(adapter4);
        //start
        reset();
    }

    private void reset() {
        //第一步
        if (items1 != null) {
            if (items1.size() > 0) {
                items1.clear();
                adapter1.notifyDataSetChanged();
            }
            for (int i = 0; i < 10; i++) {
                items1.add(new NumberBean(i, i));
            }
            adapter1.notifyDataSetChanged();
        }
        //第二步
        if (items2 != null && items2.size() > 0) {
            items2.clear();
            adapter2.notifyDataSetChanged();
        }
        if (items3 != null && items3.size() > 0) {
            items3.clear();
            adapter3.notifyDataSetChanged();
        }
        //第三步
        if (items4 != null && items4.size() > 0) {
            items4.clear();
            adapter4.notifyDataSetChanged();
        }
        //结果
        UI.clear(tvResult);
    }

    private void getResult() {
        if (items2 == null || items2.size() != 5) {
            ToastUtils.getInstance().showError(R.string.error);
            return;
        }
        if (items3 == null || items3.size() != 5) {
            ToastUtils.getInstance().showError(R.string.error);
            return;
        }
        //得到两个五位数，求差，装进rv4
        int first = 0;
        int second = 0;
        for (int i = 0; i < items2.size(); i++) {
            Object o = items2.get(i);
            if (o instanceof NumberBean) {
                NumberBean nb = (NumberBean) o;
                first = first * 10 + nb.getNumber();
            }
        }
        for (int i = 0; i < items3.size(); i++) {
            Object o = items3.get(i);
            if (o instanceof NumberBean) {
                NumberBean nb = (NumberBean) o;
                second = second * 10 + nb.getNumber();
            }
        }
        int result = Math.abs(first - second);
        if (items4 != null) {
            if (items4.size() > 0) {
                items4.clear();
                adapter4.notifyDataSetChanged();
            }
            int id = 0;
            do {
                items4.add(0, new NumberBean(id++, result % 10));
                result /= 10;
            } while (result >= 1);
            adapter4.notifyDataSetChanged();
        }
    }

    @OnClick({R.id.tv_random_change_1, R.id.tv_random_change_2, R.id.tv_reset})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_random_change_1) {
            if (random == null) random = new Random();
            for (int i = 1; i < items2.size(); i++) {
                if (random.nextBoolean()) {
                    Object o = items2.get(i);
                    items2.set(i, items2.get(i - 1));
                    items2.set(i - 1, o);
                }
            }
            adapter2.notifyDataSetChanged();
            getResult();
        } else if (id == R.id.tv_random_change_2) {
            if (random == null) random = new Random();
            for (int i = 1; i < items3.size(); i++) {
                if (random.nextBoolean()) {
                    Object o = items3.get(i);
                    items3.set(i, items3.get(i - 1));
                    items3.set(i - 1, o);
                }
            }
            adapter3.notifyDataSetChanged();
            getResult();
        } else if (id == R.id.tv_reset) {
            UI.setVisible(group);
            reset();
        }
    }
}
